#!/usr/bin/env python3
from scapy.all import sniff, IP, Ether

def packet_handler(packet):
    """解析接收的包并打印ECN信息"""
    if IP in packet:
        ip = packet[IP]
        ecn = ip.ecn
        ecn_desc = {
            0: "未使用ECN",
            1: "支持ECN（01）",
            2: "支持ECN（10）",
            3: "遭遇拥塞（11）"
        }.get(ecn, f"未知（{ecn}）")
        print(f"收到包：{ip.src} -> {ip.dst}，ECN标记：{ecn}（{ecn_desc}）")

def main():
    print("开始监听ECN包...（按Ctrl+C停止）")
    # 监听所有接口的IPv4包
    sniff(prn=packet_handler, filter="ip", store=0)

if __name__ == '__main__':
    main()